package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.RoomInfoService;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@RequiredArgsConstructor
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    private final ApartmentInfoMapper apartmentInfoMapper;

    private final GraphInfoMapper graphInfoMapper;

    private final AttrValueMapper attrValueMapper;

    private final FacilityInfoMapper facilityInfoMapper;

    private final LabelInfoMapper labelInfoMapper;

    private final PaymentTypeMapper paymentTypeMapper;

    private final LeaseTermMapper leaseTermMapper;

    private final RoomAttrValueMapper roomAttrValueMapper;

    private final RoomFacilityMapper roomFacilityMapper;

    private final RoomLabelMapper roomLabelMapper;

    private final RoomPaymentTypeMapper roomPaymentTypeMapper;

    private final RoomLeaseTermMapper roomLeaseTermMapper;

    @Override
    public Integer roomCountByApartmentId(Long id) {
        return baseMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id)).intValue();
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page,RoomQueryVo queryVo) {
        return baseMapper.pageRoomItemByQuery(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        // 1. 获取房间基本信息与所属公寓信息
        RoomInfo roomInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        // 2. 获取graphInfo
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
        List<GraphVo> graphVos = graphInfos.stream().map(graphInfo -> GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build()).collect(Collectors.toList());
        roomDetailVo.setGraphVoList(graphVos);

        // 3. attr
        List<AttrValueVo> attrValueVos = attrValueMapper.listAttrValueVoByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVos);

        // 4. facility
        List<FacilityInfo> facilityInfos = facilityInfoMapper.listFacilityInfoByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);

        // 5. label
        List<LabelInfo> labels = labelInfoMapper.listLabelInfoByRoomId(id);
        roomDetailVo.setLabelInfoList(labels);

        // 6. PaymentType
        List<PaymentType> paymentTypes = paymentTypeMapper.listPaymentTypeByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypes);

        // 7 LeaseTerm
        List<LeaseTerm> leaseTerms = leaseTermMapper.listLeaseTermByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTerms);

        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        // 1. 删除房间配套关系
        graphInfoMapper.delete(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType,ItemType.ROOM));
        roomAttrValueMapper.delete(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        roomFacilityMapper.delete(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        roomLabelMapper.delete(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        roomPaymentTypeMapper.delete(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        roomLeaseTermMapper.delete(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));

        // 2. 删除房间基本信息
        baseMapper.deleteById(id);

    }
}




